<?php 
namespace Product\Model;

 use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Predicate\In;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Predicate\Like;

use Zend\Cache\Storage\StorageInterface;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
		
 class ProductTable extends AbstractTableGateway
 {
     protected $tableGateway;
     protected $cache;
     protected $table = 'product';
     //protected $resultSetPrototype;
     
     public function __construct(Adapter $adapter,TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
         
         $this->adapter = $adapter;
         $this->resultSetPrototype = new HydratingResultSet();
        $this->resultSetPrototype->setObjectPrototype(new Product());
         $this->initialize();
     }

     public function setCache(StorageInterface $cache)
     {
     	$this->cache = $cache;
     }
     
     public function fetchAllFronend()
     {
     		$select = $this->tableGateway->getSql()->select()->order ( array (
     				'product.ID_PR DESC'
     		) );
     		$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     		$select->limit(5);
     		$select->group('product.ID_PR');
     		$select->where(array('HIGHLIGHT_PR'=>'yes'));
     		$resultSet = $this->tableGateway->selectWith ( $select );
     		$arr = $resultSet->buffer ()->toArray();
     	return $arr;
     	 
     }
     
     public function Rss($url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
     		$select = $this->tableGateway->getSql()->select()->order ( array (
     				'product.ID_PR DESC'
     		) );
     		$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     		$select->group('product.ID_PR');
     		$resultSet = $this->tableGateway->selectWith ( $select );
     		$arr = $resultSet->buffer ()->toArray();
     		$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
     }
     
     public function cungloai($id)
     {
     	$selectcl = $this->tableGateway->getSql()->select();
     	$selectcl->where(array('ID_PR'=>$id));
     	$arr_resultSetCl = $this->tableGateway->selectWith ( $selectcl )->buffer ()->toArray();
     	
     	$select = $this->tableGateway->getSql()->select()->order ( array (
     			'product.ID_PR DESC'
     	) );
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->limit(9);
     	$select->group('product.ID_PR');
     	$select->where(array('ID_PRT'=>$arr_resultSetCl[0]['ID_PRT']));
     	$resultSet = $this->tableGateway->selectWith ( $select );
     	$arr = $resultSet->buffer ()->toArray();
     	return $arr;
     	 
     }
     
     public function types($id)
     {     
     	$select = $this->tableGateway->getSql()->select()->order ( array (
     			'product.ID_PR DESC'
     	) );
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->limit(10);
     	$select->group('product.ID_PR');
     	$select->where(array('ID_PRT'=>$id));
     	$resultSet = $this->tableGateway->selectWith ( $select );
     	$arr = $resultSet->buffer ()->toArray();
     	return $arr;
     	 
     }
     
     public function fetchAllFronend1($offset,$limit,$url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
     		$select = $this->tableGateway->getSql()->select()->order ( array (
     				'product.ID_PR DESC'
     		) );
     		$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     		$select->limit($limit)->offset($offset);
     		$select->group('product.ID_PR');
     		$resultSet = $this->tableGateway->selectWith ( $select );
     		$arr = $resultSet->buffer ()->toArray();
     		$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
     	 
     }
     
     public function fetchAllFronendvideohot()
     {
     		$select = $this->tableGateway->getSql()->select()->order ( array (
     				'product.VIEW DESC'
     		) );
     		$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     		$select->limit(5);
     		$select->group('product.ID_PR');
     		$select->where(array('product.ID_PRT'=>'2'));
     		$resultSet = $this->tableGateway->selectWith ( $select );
     		$arr = $resultSet->buffer ()->toArray();

     	return $arr;
     	 
     }
     
     public function fetchAllFronendtypehot($id)
     {
     	$select = $this->tableGateway->getSql()->select()->order ( array (
     			'product.VIEW DESC'
     	) );
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->limit(5);
     	$select->group('product.ID_PR');
     	$select->where(array('product.ID_PRT'=>$id));
     	$resultSet = $this->tableGateway->selectWith ( $select );
     	$arr = $resultSet->buffer ()->toArray();
     
     	return $arr;
     	 
     }
     
     public function fetchAllFronendtruyenhot()
     {
     	$select = $this->tableGateway->getSql()->select()->order ( array (
     			'product.VIEW DESC'
     	) );
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->limit(5);
     	$select->group('product.ID_PR');
     	$select->where(array('product.ID_PRT'=>'1'));
     	$resultSet = $this->tableGateway->selectWith ( $select );
     	$arr = $resultSet->buffer ()->toArray();
     
     	return $arr;
     	 
     }
     
     public function fetchAll($offset,$limit,$url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
		         $select = $this->tableGateway->getSql()->select()->order ( array (
						'product.ID_PR DESC' 
				) );
		         $select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
		         $select->limit($limit)->offset($offset);
		         $select->group('product.ID_PR');
		     	$resultSet = $this->tableGateway->selectWith ( $select );
		     	$arr = $resultSet->buffer ()->toArray();
		     	$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
         
        /*  $select = $this->tableGateway->getSql()->select();
         $select->join('imgproduct','imgproduct.ID_PR=product.ID_PR' );
         $select->where(array('product.ID_PR'=>165));
         $resultSet = $this->tableGateway->selectWith ( $select );
         $resultSet->buffer ();
         return $resultSet; */
     }
     
    /*  public function xoacache(){
     	
     	$this->cache->removeItems(array());
     	
     } */
     
     public function countAll() {
     	$select = $this->tableGateway->getSql()->select();
         $select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
         $select->group('product.ID_PR');
     	$resultSet = $this->tableGateway->selectWith ( $select )->count();
     	return $resultSet;
     }
     
     public function fetchSearchAll($offset,$limit,$search,$url)
     {
     	if( ($arr = $this->cache->getItem($url)) == null) {
		     	$where = new \Zend\Db\Sql\Where();
		     	$like_name = $where->like('NAME_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_content = $where->like('CONTENT_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_highlight = $where->like('HIGHLIGHT_PR','%'.$search.'%');
		     	$where->OR;
		     	$like_id = $where->like('product.ID_PR','%'.$search.'%');
		     	$select = $this->tableGateway->getSql()->select()->order ( array (
		     			'product.ID_PR DESC'
		     	) );
		     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
		     	$select->limit($limit)->offset($offset);
		     	$select->group('product.ID_PR');
		     	$select->where($where);
		     	$resultSet = $this->tableGateway->selectWith ( $select );
		     	$arr = $resultSet->buffer ()->toArray();
				$this->cache->setItem($url,  $arr );
     	}
     	return $arr;
     }
     
     public function countSearchAll($search) {
     	$where = new \Zend\Db\Sql\Where();
     	$like_name = $where->like('NAME_PR','%'.$search.'%');
     	$where->OR;
     	$like_content = $where->like('CONTENT_PR','%'.$search.'%');
     	$where->OR;
     	$like_highlight = $where->like('HIGHLIGHT_PR','%'.$search.'%');
     	$where->OR;
     	$like_id = $where->like('product.ID_PR','%'.$search.'%');
     	$select = $this->tableGateway->getSql()->select();
     	$select->join(array('imgproduct' => 'imgproduct'), 'product.ID_PR = imgproduct.ID_PR', array('LINKIMG'), 'inner');
     	$select->group('product.ID_PR');
     	$select->where($where);
     	$resultSet = $this->tableGateway->selectWith ( $select )->count();
     	return $resultSet;
     }
     
     public function getProduct($id)
     {
         $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('ID_PR' => $id));
         $row = $rowset->current();
        
         if ($row) {
             $row->namepro = $row->NAME_PR;
	         $row->contentpro = $row->CONTENT_PR;
	         $row->activepro = $row->ACTIVE_PR;
	         $row->producttypes = $row->ID_PRT;
	         $row->idproduct = $row->ID_PR;
         }        
         return $row;     	
     }

     public function getProductInType($idtype)
     {
     	$idtype  = (int) $idtype;
     	$resultSet = $this->tableGateway->select(array('ID_PRT' => $idtype));
		$resultSet = $resultSet->toArray();
		$arr = array();
		foreach ($resultSet as $val)
			$arr[] = $val['ID_PR'];
		return $arr;
     }
     
     public function getTypeProduct($idtype)
     {
     	$idtype  = (int) $idtype;
     	$resultSet = $this->tableGateway->select(array('ID_PRT' => $idtype));
     	$resultSet = $resultSet->toArray();

     	return $resultSet;
     }
     
     public function saveProduct(Product $product,$id)
     {
     	if($product->activepro == null)
     		$product->activepro = 0;  
     	$arr = array('bubu','anh day','oh year','yanme','keke289','tumlumtun','kiss23','hot9','aloala','kute9','uhuhu','thietla','yumne','khongai'
     			,'xuan87','hethuxe83','love','texttiss');     
        $arr_random = array_rand($arr,1) ;
         if ($id == 0) {
         	$data = array(
         			'ID_PRT' => $product->producttypes,
         			'NAME_PR' => $product->namepro ,
         			'CONTENT_PR' => $product->contentpro ,
         			'ACTIVE_PR' => $product->activepro ,
         			'DATE_PR' => date("d-m-Y") ,
         			'AUTHOR' => $arr[$arr_random],
         			'VIEW' => rand(300,900),
         	);
         	
             $this->tableGateway->insert($data);
             $select = $this->tableGateway->getSql()->select();
             $select->columns(array(
                   'ID_PR' => new Expression('MAX(ID_PR)')
	          ));
	         $resultSet = $this->tableGateway->selectWith ( $select );
	         $row = $resultSet->current()->ID_PR;
	         return  $row;
         }else {
         	$data = array(
         			'ID_PRT' => $product->producttypes,
         			'NAME_PR' => $product->namepro ,
         			'CONTENT_PR' => $product->contentpro ,
         			'ACTIVE_PR' => $product->activepro ,
         	);
         	
             if ($this->getProduct($id)) {
                 $this->tableGateway->update($data, array('ID_PR' => $id));
             } else {
                 throw new \Exception('Product id does not exist');
             }
         }
     }

     public function updatehighlights($id,$idquery){
     	if($idquery == 'no')
     		$idquery='yes';
     	else 
     		$idquery='no';
     	$data = array(
     			'HIGHLIGHT_PR' => $idquery,
     	);
     	$this->tableGateway->update($data, array('ID_PR' => $id));
     }
     
     public function deleteProduct($id)
     {
         $this->tableGateway->delete(array('ID_PR' => (int) $id));
     }
     
     public function deleteAllProduct($arrid)
     {
     	$where = new In('ID_PR',$arrid);
     	$this->tableGateway->delete($where);
     }
     
     public function deleteProductInProductType($id)
     {
     	$this->tableGateway->delete(array('ID_PRT' => (int) $id));
     }
 }